package com.jkj.http.filter;

import org.apache.cxf.transport.servlet.CXFServlet;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

@WebFilter(filterName = "myFileter", urlPatterns = "/*")
public class ServletFilter implements Filter {
    private Map<String, ? extends ServletRegistration> listBean;
    private ServletContext servletContext;

    public void destroy() {
        // TODO Auto-generated method stub
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        //  String requestURI = request.getRequestURI();
        // System.out.println("成功啦。。。, 请求URI是:" + requestURI);
        chain.doFilter(request, response);
    }


    public void init(FilterConfig arg0) throws ServletException {
        // TODO Auto-generated method stub
        listBean = arg0.getServletContext().getServletRegistrations();
        if (listBean.get("CXFServlet") == null) {
            servletContext = arg0.getServletContext();//
            ServletRegistration.Dynamic dynamic = servletContext.addServlet("CXFServlet", new CXFServlet());//注意此处和第4步的代码是同样的功能
            dynamic.addMapping("/service/*");
        }
    }
}
